﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using DataLayer.Model.Objects;
using DataLayer.Model.Classes;
using DataLayer.Properties;
using Action = DataLayer.Model.Objects.Action;

namespace DataLayer
{
    public class Singleton
    {
      

        private static string _skillDataFileName = string.Empty;
        public static string SkillDataFileName
        {
            get { return _skillDataFileName; }
            set
            {
                _skillDataFileName = value;
                _classes = XmlEngine.ReadFromFile<Occupations>(SkillDataFileName);
            }
        }

        private static string _commandDataFileName = string.Empty;
        public static string CommandDataFileName
        {
            get { return _commandDataFileName; }
            set
            {
                _commandDataFileName = value;
                _commands = XmlEngine.ReadFromFile<Commands>(CommandDataFileName);
            }
        }

        private static string _settingsFileName = string.Empty;
        public static string SettingsFileName
        {
            get { return _settingsFileName; }
            set
            {
                _settingsFileName = value;
                _settings = XmlEngine.ReadFromFile<ApplicationSettings>(_settingsFileName);
            }
        }

        private static Terminal _terminal;
        public static Terminal Terminal{
            get {
                return _terminal ?? new Terminal();
            }
        }


        private static Occupations _classes;
        public static Occupations Classes { get { return _classes; } }

        private static Commands _commands;
        public static Commands Commands { get { return _commands; } }

        private static ApplicationSettings _settings;
        public static ApplicationSettings Settings { get { return _settings; } }

        public static void CheckDirectory(string path)
        {
            var dir = new DirectoryInfo(path);
            if (!dir.Exists) dir.Create();
        }

        public static IList<Skill> ReadClassSkills(Type pClassType)
        {
            if (pClassType.BaseType != typeof(Character))
            {
                var skills = new List<Skill>();
                foreach (var charClass in Classes.Occupation)
                    if (charClass.Name.Equals(pClassType.Name))
                    {
                        foreach (var skill in charClass.Skills)
                            skills.Add(Converter.XmlSkillClassToSkill(skill));
                        break;
                    }
                skills.AddRange(ReadClassSkills(pClassType.BaseType));
                return skills;
            }
            else
            {
                return new List<Skill>();
            }
        }
        public static IList<Action> ReadAllActions()
        {
            return Commands.Command.Select(command => new Command
                {
                    Name = command.Name, Target = String.Empty
                }).Cast<Action>().ToList();
        }

        public static void Init()
        {
            SkillDataFileName = Resources.skillDataFile;
            CommandDataFileName = Resources.commandDataFile;
            SettingsFileName = Resources.settingsDataFile;
        }

        public static IList<Type> GetChildClasses(Type pType)
        {
            return Assembly.GetAssembly(pType).GetTypes().Where(t => t.IsSubclassOf(pType)).Where(t => t.BaseType == pType).ToList();

        }


        public static bool AlreadeExists(string profileName)
        {
            //TODO: add settongs file to application
            var profiles = Directory.GetFiles(@".\Profiles").ToList();
            profileName += ".provile";
            return profiles.Contains(profileName);

        }
    }
}
